package com.shoe.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shoe.mapper.UCouponMapper;
import com.shoe.pojo.AdminCouponPojo;
import com.shoe.pojo.UserGetCouPojo;
import com.shoe.service.UCouponService;
import com.shoe.util.ResultUtil;
import com.shoe.vo.ResponseResult;
import com.shoe.vo.UserCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class UCouponServiceImpl implements UCouponService {

    @Autowired
    UCouponMapper uCouponMapper;

    @Autowired
    ResultUtil resultUtil;

    @Override
    public ResponseResult selectUCouponByCondition(Integer pageNum, Integer pageSize, Integer UId, String CType, Integer CMoney, Integer CHolding) {
        List<UserCouponVo> uCouponVoList = uCouponMapper.selectUCouponByCondition(UId, CType, CMoney, CHolding);
        if (null != uCouponVoList && uCouponVoList.size() > 0) {
            PageInfo<UserCouponVo> uCouponVoPageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
                uCouponMapper.selectUCouponByCondition(UId, CType, CMoney, CHolding);
            });
            System.out.println("service" + uCouponVoPageInfo.toString());
            return resultUtil.getResponseResult(0, "查询成功", uCouponVoList.size(), uCouponVoPageInfo);
        } else {
            return resultUtil.getResponseResult(2, "没有结果");
        }
    }

    @Override
    public UserGetCouPojo selectUCouHave(Integer userId, Integer userCouId) {
        return uCouponMapper.selectUCouHave(userId, userCouId);
    }

    @Override
    public List<AdminCouponPojo> selectUCouShow(Integer couponId) {
        return uCouponMapper.selectUCouShow(couponId);
    }

    @Override
    public int addUGetCou(UserGetCouPojo userGetCouPojo) {
        return uCouponMapper.addUGetCou(userGetCouPojo);
    }

    @Override
    public int deleUCou(Integer userCouId) {
        return uCouponMapper.deleUCou(userCouId);
    }

    @Transactional
    @Override
    public ResponseResult delUCouDate(Integer userId) {

        int sum = 0;
        int count = 0;

        List<UserGetCouPojo> uGetCouDate = uCouponMapper.selectUCouDate(userId);
        for (UserGetCouPojo uOneGetCouDate : uGetCouDate) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //创建日期转换对象：年 月 日
            Date today = new Date();    //今天 实际日期是  2018-11-12    Debug：Wed Nov 12 12:08:12 CST 2018
            Date dateD = null; //将字符串转换为 date 类型  Debug：Sun Nov 11 00:00:00 CST 2018

//            System.out.println("到期日期 -> " + uOneGetCouDate.getUsercouponDate());
//            System.out.println("今天日期 -> " + sdf.format(today));

            try {
                dateD = sdf.parse(uOneGetCouDate.getUsercouponDate());  //将字符串转换为 date 类型  Debug：Sun Nov 11 00:00:00 CST 2018
//                boolean flag = dateD.getTime() >= today.getTime();
                if (today.getTime() >= dateD.getTime()) {
                    count++;
                    int affRow = uCouponMapper.deleUCou(uOneGetCouDate.getUsercouponId());
                    sum += affRow;

                    System.out.println("count -> " + count);
                    System.out.println("sum -> " + sum);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (sum == count && sum > 0) {
            return resultUtil.getResponseResult(0, "", sum);
        } else if (sum == count && sum == 0) {
            return resultUtil.getResponseResult(1, "", sum);
        } else {
            return resultUtil.getResponseResult(2, "", sum);
        }
    }

}
